package com.action;

import java.util.List;

import com.hdb.DB;
import com.hdb.TAdmin;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;

@ParentPackage("default")
@Namespace("/admin")
@Results({
        @Result(location = "/admin/admin/adminManage.jsp"),
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "edit", location = "/admin/admin/adminEditPage.jsp"),
        @Result(name = "adminIndex", location = "/admin/index.jsp"),
})
@InterceptorRef("adminStack")/*设置过滤器*/
public class AdminAction extends BasicAction {
    private int adminId;
    private String adminName;
    private String adminPw;

    private short adminLevel;
    private String message;
    private String path;

    private int index = 1;

    private DB db;

    /*
     * 添加管理员
     * 通过设置adminName和adminPw添加新的管理员
     */
    @Action("adminAdd")
    public String adminAdd() {
        TAdmin admin = new TAdmin();
        admin.setAdminName(adminName);
        if (adminPw.trim().length() < 6) {
            setMessage("密码至少6位");
            setPath("admin/admin/adminAdd.jsp");
            return "succeed";
        }
        admin.setAdminPw(adminPw);
        admin.setAdminLevel(adminLevel);
        if (db.adminDAO.save(admin)) {
            this.setMessage("操作成功");
        } else {
            this.setMessage("操作失败");
        }
        this.setPath("admin/adminManage.action");
        return "succeed";
    }
    /*管理员登录后首页*/
    @Action("adminLogin")
    public String adminLogin() {
        return "adminIndex";
    }
    /*管理登出*/
    @Action("adminLogout")
    public String adminLogout() {
        setAdmin(null);
        return "index";
    }
    /*编辑管理员信息*/
    @Action("adminEdit")
    public String adminEdit() {
        TAdmin admin = db.adminDAO.findById(adminId);
        if (adminPw.trim().length() >= 6) {
            admin.setAdminPw(adminPw);
        }
        admin.setAdminLevel(adminLevel);
        if (db.adminDAO.attach(admin)) {
            this.setMessage("操作成功");
        } else {
            this.setMessage("操作失败");
        }
        this.setPath("admin/adminManage.action");
        return "succeed";
    }

    /*
     * 管理员工与管理员
     * 列出所有的员工与管理员
     */
    @Action("adminManage")
    public String adminManage() {
        List adminList = db.adminDAO.findAll(0, 20);
        getRequest().put("adminList", adminList);
        return ActionSupport.SUCCESS;
    }

    /*
     * 删除员工或管理员
     * 通过userId删除对应管理员
     */
    @Action("adminDel")
    public String adminDel() {
        TAdmin admin = db.adminDAO.findById(adminId);
        if (admin != null) {
            if (db.adminDAO.delete(admin)) {
                this.setMessage("删除成功");
            } else {
                this.setMessage("删除失败");
            }
        } else {
            this.setMessage("删除对象不存在");
        }
        this.setPath("admin/adminManage.action");
        return "succeed";
    }
    /*
     * 删除员工或管理员
     * 通过userId删除对应管理员
     */
    @Action("adminEditPage")
    public String adminEditPage() {
        TAdmin admin = db.adminDAO.findById(adminId);
        if (admin != null) {
            getRequest().put("admin", admin);
        } else {
            this.setMessage("对象不存在");
        }
        return "edit";
    }
    public int getAdminId() {
        return adminId;
    }

    public void setAdminId(int adminId) {
        this.adminId = adminId;
    }

    public String getAdminName() {
        return adminName;
    }

    public void setAdminName(String adminName) {
        this.adminName = adminName;
    }

    public String getAdminPw() {
        return adminPw;
    }

    public void setAdminPw(String adminPw) {
        this.adminPw = adminPw;
    }

    public short getAdminLevel() {
        return adminLevel;
    }

    public void setAdminLevel(short adminLevel) {
        this.adminLevel = adminLevel;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }
}
